home *** CD-ROM | disk | FTP | other *** search
/ Java Interactive Reference Guide / Java Interactive Reference Guide.iso / autorun / source_d.dir / 00062_15.txt < prev    next >
Encoding:
Text File  |  1980-01-11  |  8.6 KB  |  341 lines

  1. import java.util.*;
  2. import java.awt.*;
  3. import java.applet.Applet;
  4.  
  5. class Node {
  6.     double x;
  7.     double y;
  8.  
  9.     double dx;
  10.     double dy;
  11.  
  12.     boolean fixed;
  13.  
  14.     String lbl;
  15. }
  16.  
  17. class Edge {
  18.     int from;
  19.     int to;
  20.  
  21.     double len;
  22. }
  23.  
  24. class GraphPanel extends Panel implements Runnable {
  25.     Graph graph;
  26.     int nnodes;
  27.     Node nodes[] = new Node[100];
  28.  
  29.     int nedges;
  30.     Edge edges[] = new Edge[200];
  31.  
  32.     Thread relaxer;
  33.     boolean stress;
  34.     boolean random;
  35.  
  36.     GraphPanel(Graph graph) {
  37.     this.graph = graph;
  38.     }
  39.  
  40.     int findNode(String lbl) {
  41.     for (int i = 0 ; i < nnodes ; i++) {
  42.         if (nodes[i].lbl.equals(lbl)) {
  43.         return i;
  44.         }
  45.     }
  46.     return addNode(lbl);
  47.     }
  48.     int addNode(String lbl) {
  49.     Node n = new Node();
  50.     n.x = 10 + 380*Math.random();
  51.     n.y = 10 + 380*Math.random();
  52.     n.lbl = lbl;
  53.     nodes[nnodes] = n;
  54.     return nnodes++;
  55.     }
  56.     void addEdge(String from, String to, int len) {
  57.     Edge e = new Edge();
  58.     e.from = findNode(from);
  59.     e.to = findNode(to);
  60.     e.len = len;
  61.     edges[nedges++] = e;
  62.     }
  63.  
  64.     public void run() {
  65.     while (true) {
  66.         relax();
  67.         if (random && (Math.random() < 0.03)) {
  68.         Node n = nodes[(int)(Math.random() * nnodes)];
  69.         if (!n.fixed) {
  70.             n.x += 100*Math.random() - 50;
  71.             n.y += 100*Math.random() - 50;
  72.         }
  73.         graph.play(graph.getCodeBase(), "audio/drip.au");
  74.         }
  75.         try {
  76.         Thread.sleep(100);
  77.         } catch (InterruptedException e) {
  78.         break;
  79.         }
  80.     }
  81.     }
  82.  
  83.     synchronized void relax() {
  84.     for (int i = 0 ; i < nedges ; i++) {
  85.         Edge e = edges[i];
  86.         double vx = nodes[e.to].x - nodes[e.from].x;
  87.         double vy = nodes[e.to].y - nodes[e.from].y;
  88.         double len = Math.sqrt(vx * vx + vy * vy);
  89.         double f = (edges[i].len - len) / (len * 3) ;
  90.         double dx = f * vx;
  91.         double dy = f * vy;
  92.  
  93.         nodes[e.to].dx += dx;
  94.         nodes[e.to].dy += dy;
  95.         nodes[e.from].dx += -dx;
  96.         nodes[e.from].dy += -dy;
  97.     }
  98.  
  99.     for (int i = 0 ; i < nnodes ; i++) {
  100.         Node n1 = nodes[i];
  101.         double dx = 0;
  102.         double dy = 0;
  103.  
  104.         for (int j = 0 ; j < nnodes ; j++) {
  105.         if (i == j) {
  106.             continue;
  107.         }
  108.         Node n2 = nodes[j];
  109.         double vx = n1.x - n2.x;
  110.         double vy = n1.y - n2.y;
  111.         double len = vx * vx + vy * vy;
  112.         if (len == 0) {
  113.             dx += Math.random();
  114.             dy += Math.random();
  115.         } else if (len < 100*100) {
  116.             dx += vx / len;
  117.             dy += vy / len;
  118.         }
  119.         }
  120.         double dlen = dx * dx + dy * dy;
  121.         if (dlen > 0) {
  122.         dlen = Math.sqrt(dlen) / 2;
  123.         n1.dx += dx / dlen;
  124.         n1.dy += dy / dlen;
  125.         }
  126.     }
  127.  
  128.     Dimension d = size();
  129.     for (int i = 0 ; i < nnodes ; i++) {
  130.         Node n = nodes[i];
  131.         if (!n.fixed) {
  132.         n.x += Math.max(-5, Math.min(5, n.dx));
  133.         n.y += Math.max(-5, Math.min(5, n.dy));
  134.         //System.out.println("v= " + n.dx + "," + n.dy);
  135.         if (n.x < 0) {
  136.             n.x = 0;
  137.         } else if (n.x > d.width) {
  138.             n.x = d.width;
  139.         }
  140.         if (n.y < 0) {
  141.             n.y = 0;
  142.         } else if (n.y > d.height) {
  143.             n.y = d.height;
  144.         }
  145.         }
  146.         n.dx /= 2;
  147.         n.dy /= 2;
  148.     }
  149.     repaint();
  150.     }
  151.  
  152.     Node pick;
  153.     boolean pickfixed;
  154.     Image offscreen;
  155.     Dimension offscreensize;
  156.     Graphics offgraphics;
  157.     
  158.  
  159.     final Color fixedColor = Color.red;
  160.     final Color selectColor = Color.pink;
  161.     final Color edgeColor = Color.black;
  162.     final Color nodeColor = new Color(250, 220, 100);
  163.     final Color stressColor = Color.gray;
  164.     final Color arcColor1 = Color.black;
  165.     final Color arcColor2 = Color.pink;
  166.     final Color arcColor3 = Color.red;
  167.  
  168.     public void paintNode(Graphics g, Node n, FontMetrics fm) {
  169.     int x = (int)n.x;
  170.     int y = (int)n.y;
  171.     g.setColor((n == pick) ? selectColor : (n.fixed ? fixedColor : nodeColor));
  172.     int w = fm.stringWidth(n.lbl) + 10;
  173.     int h = fm.getHeight() + 4;
  174.     g.fillRect(x - w/2, y - h / 2, w, h);
  175.     g.setColor(Color.black);
  176.     g.drawRect(x - w/2, y - h / 2, w-1, h-1);
  177.     g.drawString(n.lbl, x - (w-10)/2, (y - (h-4)/2) + fm.getAscent());
  178.     }
  179.  
  180.     public synchronized void update(Graphics g) {
  181.     Dimension d = size();
  182.     if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) {
  183.         offscreen = createImage(d.width, d.height);
  184.         offscreensize = d;
  185.         offgraphics = offscreen.getGraphics();
  186.         offgraphics.setFont(getFont());
  187.     }
  188.  
  189.     offgraphics.setColor(getBackground());
  190.     offgraphics.fillRect(0, 0, d.width, d.height);
  191.     for (int i = 0 ; i < nedges ; i++) {
  192.         Edge e = edges[i];
  193.         int x1 = (int)nodes[e.from].x;
  194.         int y1 = (int)nodes[e.from].y;
  195.         int x2 = (int)nodes[e.to].x;
  196.         int y2 = (int)nodes[e.to].y;
  197.         int len = (int)Math.abs(Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) - e.len);
  198.         offgraphics.setColor((len < 10) ? arcColor1 : (len < 20 ? arcColor2 : arcColor3)) ;
  199.         offgraphics.drawLine(x1, y1, x2, y2);
  200.         if (stress) {
  201.         String lbl = String.valueOf(len);
  202.         offgraphics.setColor(stressColor);
  203.         offgraphics.drawString(lbl, x1 + (x2-x1)/2, y1 + (y2-y1)/2);
  204.         offgraphics.setColor(edgeColor);
  205.         }
  206.     }
  207.  
  208.     FontMetrics fm = offgraphics.getFontMetrics();
  209.     for (int i = 0 ; i < nnodes ; i++) {
  210.         paintNode(offgraphics, nodes[i], fm);
  211.     }
  212.  
  213.     g.drawImage(offscreen, 0, 0, null);
  214.     }
  215.  
  216.     public synchronized boolean mouseDown(Event evt, int x, int y) {
  217.     double bestdist = Double.MAX_VALUE;
  218.     for (int i = 0 ; i < nnodes ; i++) {
  219.         Node n = nodes[i];
  220.         double dist = (n.x - x) * (n.x - x) + (n.y - y) * (n.y - y);
  221.         if (dist < bestdist) {
  222.         pick = n;
  223.         bestdist = dist;
  224.         }
  225.     }
  226.     pickfixed = pick.fixed;
  227.     pick.fixed = true;
  228.     pick.x = x;
  229.     pick.y = y;
  230.     repaint();
  231.     return true;
  232.     }
  233.  
  234.     public synchronized boolean mouseDrag(Event evt, int x, int y) {
  235.     pick.x = x;
  236.     pick.y = y;
  237.     repaint();
  238.     return true;
  239.     }
  240.  
  241.     public synchronized boolean mouseUp(Event evt, int x, int y) {
  242.     pick.x = x;
  243.     pick.y = y;
  244.     pick.fixed = pickfixed;
  245.     pick = null;
  246.     
  247.     repaint();
  248.     return true;
  249.     }
  250.  
  251.     public void start() {
  252.     relaxer = new Thread(this);
  253.     relaxer.start();
  254.     }
  255.     public void stop() {
  256.     relaxer.stop();
  257.     }
  258. }
  259.  
  260. public class Graph extends Applet {
  261.     GraphPanel panel;
  262.  
  263.     public void init() {
  264.     setLayout(new BorderLayout());
  265.  
  266.     panel = new GraphPanel(this);
  267.     add("Center", panel);
  268.     Panel p = new Panel();
  269.     add("South", p);
  270.     p.add(new Button("Scramble"));
  271.     p.add(new Button("Shake"));
  272.     p.add(new Checkbox("Stress"));
  273.     p.add(new Checkbox("Random"));
  274.  
  275.     String edges = getParameter("edges");
  276.     for (StringTokenizer t = new StringTokenizer(edges, ",") ; t.hasMoreTokens() ; ) {
  277.         String str = t.nextToken();
  278.         int i = str.indexOf('-');
  279.         if (i > 0) {
  280.         int len = 50;
  281.         int j = str.indexOf('/');
  282.         if (j > 0) {
  283.             len = Integer.valueOf(str.substring(j+1)).intValue();
  284.             str = str.substring(0, j);
  285.         }
  286.         panel.addEdge(str.substring(0,i), str.substring(i+1), len);
  287.         }
  288.     }
  289.     Dimension d = size();
  290.     String center = getParameter("center");
  291.     if (center != null){
  292.         Node n = panel.nodes[panel.findNode(center)];
  293.         n.x = d.width / 2;
  294.         n.y = d.height / 2;
  295.         n.fixed = true;
  296.     }
  297.     }
  298.  
  299.     public void start() {
  300.     panel.start();
  301.     }
  302.     public void stop() {
  303.     panel.stop();
  304.     }
  305.     public boolean action(Event evt, Object arg) {
  306.     if (arg instanceof Boolean) {
  307.         if (((Checkbox)evt.target).getLabel().equals("Stress")) {
  308.         panel.stress = ((Boolean)arg).booleanValue();
  309.         } else {
  310.         panel.random = ((Boolean)arg).booleanValue();
  311.         }
  312.         return true;
  313.     } 
  314.     if ("Scramble".equals(arg)) {
  315.         play(getCodeBase(), "audio/computer.au");
  316.         Dimension d = size();
  317.         for (int i = 0 ; i < panel.nnodes ; i++) {
  318.         Node n = panel.nodes[i];
  319.         if (!n.fixed) {
  320.             n.x = 10 + (d.width-20)*Math.random();
  321.             n.y = 10 + (d.height-20)*Math.random();
  322.         }
  323.         }
  324.         return true;
  325.     }
  326.     if ("Shake".equals(arg)) {
  327.         play(getCodeBase(), "audio/gong.au");
  328.         Dimension d = size();
  329.         for (int i = 0 ; i < panel.nnodes ; i++) {
  330.         Node n = panel.nodes[i];
  331.         if (!n.fixed) {
  332.             n.x += 80*Math.random() - 40;
  333.             n.y += 80*Math.random() - 40;
  334.         }
  335.         }
  336.         return true;
  337.     }
  338.     return false;
  339.     }
  340. }
  341.